package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.AwardsMapper;
import com.px.dal.CompanyUsersMapper;
import com.px.dal.ProjectInfoMapper;
import com.px.dal.ReviewGroupInfoMapper;
import com.px.model.AwardInfo;
import com.px.model.CompanyUsers;
import com.px.model.ProjectInfo;
import com.px.model.ReviewGroupInfo;
import com.px.model.ReviewProjectRel;
import com.px.model.getData;
import com.px.unit.Response;

//把当前类注解为控制器
@Controller
public class ProjectInfoController {
	// 实例化我们的数据操作类
	ProjectInfoMapper project_mapper = new ProjectInfoMapper();
	AwardsMapper award_mapper = new AwardsMapper();
	CompanyUsersMapper companyusers_mapper = new CompanyUsersMapper();
	ReviewGroupInfoMapper reviewtb_mapper = new ReviewGroupInfoMapper();

	/**
	 * 获取所有项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllProjects", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllProjects() {
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<ProjectInfo> projectInfos = project_mapper.getAllProjects();
		mydata.setProjectInfolist(projectInfos);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 获取指定Id项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getProjectByProjectInfoId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getProjectByProjectInfoId(@RequestBody ProjectInfo projectInfo) {
		if (projectInfo != null) {
			// 判断查询的项目是否存在
			int checkid = project_mapper.checkProjectInfoIdIsExist(projectInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该项目不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<ProjectInfo> projectInfos = project_mapper.getProjectByProjectInfoId(projectInfo);
				mydata.setProjectInfolist(projectInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	/**
	 * 根据公司员工id查找项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getProjectByCompanyUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getProjectByCompanyUserId(@RequestBody ProjectInfo projectInfo) {
		if (projectInfo != null) {
			CompanyUsers companyUsers = new CompanyUsers();
			companyUsers.setCompany_user_id(projectInfo.getCompany_user_id());
			// 判断查询的项目是否存在
			int checkid = companyusers_mapper.checkCompanyUsersIdIsExist(companyUsers);
			if (!(checkid > 0)) {
				return new Response(304, "该员工不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<ProjectInfo> projectInfos = project_mapper.getProjectByCompanyUserId(projectInfo);
				mydata.setProjectInfolist(projectInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	/**
	 * 添加项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/addProjectInfo", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response addProjectInfo(@RequestBody ProjectInfo projectInfo) {

		if (projectInfo != null) {
			projectInfo.setProject_check(0);
			projectInfo.setCounts(0);
			projectInfo.setAverage(0);
			int counts = project_mapper.insertProject(projectInfo);
			if (counts > 0) {
				return new Response(200, "添加成功！");
			} else {
				return new Response(304, "添加失败！");
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 删除项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteProjectByProjectInfoId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteProjectByProjectInfoId(@RequestBody ProjectInfo projectInfo) {

		if (projectInfo != null) {

			// 判断删除的项目是否存在
			int checkid = project_mapper.checkProjectInfoIdIsExist(projectInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该项目不存在");
			} else {
				int counts = project_mapper.deleteProjectByProjectInfoId(projectInfo);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 修改项目信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateProjectByProjectInfoId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateProjectByProjectInfoId(@RequestBody ProjectInfo projectInfo) {

		if (projectInfo != null) {

			// 判断修改的项目是否存在
			int checkid = project_mapper.checkProjectInfoIdIsExist(projectInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该项目不存在");
			} else {
				int counts = project_mapper.updateProjectByProjectInfoId(projectInfo);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	// 查询指定奖项id的项目信息
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getProjectsByAwardId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getProjectsByAwardId(@RequestBody AwardInfo awardInfo) {
		if (awardInfo != null) {
			// 判断查询的奖项是否存在
			int checkid = award_mapper.checkAdwardsIdIsExist(awardInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该奖项不存在");
			} else {
				List<ProjectInfo> projectInfos = project_mapper.getProjectsByAwardId(awardInfo.getAwards_id());
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				mydata.setProjectByAwardId(projectInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	// 根据评审组id查询所有项目信息
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getProjectsByReviewGroupId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getProjectsByReviewGroupId(@RequestBody ReviewProjectRel reviewProjectRel) {
		if (reviewProjectRel != null) {
			ReviewGroupInfo reviewGroupInfo = new ReviewGroupInfo();
			reviewGroupInfo.setReview_id(reviewProjectRel.getReview_id());
			// 判断查询的评审组是否存在
			int checkid = reviewtb_mapper.checkReviewIdIsExist(reviewGroupInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该评审组不存在");
			} else {
				List<ProjectInfo> projectInfos = project_mapper
						.getProjectsByReviewGroupId(reviewProjectRel.getReview_id());
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				mydata.setProjectInfolist(projectInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

}
